//
//  TTHomeListViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/8/17.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTHomeListViewCell.h"
#import <UIImageView+WebCache.h>

@interface TTHomeListViewCell ()

@property (nonatomic, strong) UIImageView *leftImageView;
@property (nonatomic, strong) UILabel *titleLabel;  // 标题
@property (nonatomic, strong) UILabel *priceLabel;  // 价格
@property (nonatomic, strong) UILabel *bottomLabel;

@property (nonatomic, strong) UIButton *cancelCollectionBtn;    // 取消收藏
@property (nonatomic, strong) UIImageView *selloutImageView;    // 售罄图片
@property (nonatomic, strong) UIView *lineView;     // 线条

@property (nonatomic, strong) NSMutableArray *labelsArray;

@end

@implementation TTHomeListViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.labelsArray = [NSMutableArray array];
    }
    return self;
}

- (void)setupTopSubviews
{
    [self.leftImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(10);
        make.width.height.mas_equalTo(145 * kScale);
    }];
    
    [self.selloutImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(80);
        make.center.equalTo(self.leftImageView);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.leftImageView).mas_offset(6);
        make.left.equalTo(self.leftImageView.mas_right).mas_offset(10);
        make.right.lessThanOrEqualTo(self.contentView.mas_right).mas_offset(-10);
    }];
    
}

// 取消收藏
- (void)cancelCollectionBtnClick
{
    if ([self.delegate respondsToSelector:@selector(cancelCollectionBtnClickWithViewCell:)]) {
        [self.delegate cancelCollectionBtnClickWithViewCell:self];
    }
}

- (void)setModel:(TTHomeListModel *)model pageType:(NSInteger)pageType
{
    self.model = model;
    
    self.priceLabel.hidden = YES;
    self.bottomLabel.hidden = YES;
    self.selloutImageView.hidden = YES;
    // 1 : @"足迹", 2 : @"收藏";
    self.cancelCollectionBtn.hidden = pageType == 1;
}

- (void)setModel:(TTHomeListModel *)model
{
    _model = model;
    
    [self setupTopSubviews];
    
    [self.leftImageView sd_setImageWithURL:[NSURL URLWithString:model.GoodsImage] placeholderImage:[UIImage imageNamed:@"f_placeholder_img"] options:SDWebImageRetryFailed];
    
    self.titleLabel.text = model.GoodsTitle;
    
    if (model.StockCount.intValue == 0) {
        self.bottomLabel.text = [NSString stringWithFormat:@"已售:%@ / 定制库存:%ld件", model.SalesVolume, (long)model.CustomizedCount];
    } else {
        self.bottomLabel.text = [NSString stringWithFormat:@"已售:%@ / 库存:%@件", model.SalesVolume, model.StockCount];
    }
    
    
    
    self.selloutImageView.hidden = (model.StockCount.intValue > 0 || model.CustomizedCount > 0);
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"￥%@", model.Price]];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:11] range:NSMakeRange(0, 1)];
    self.priceLabel.attributedText = attrStr;
    
    // 防止cell中多次添加label
    for (UILabel *label in self.labelsArray) {
        [label removeFromSuperview];
    }
    [self.labelsArray removeAllObjects];
    
    NSMutableArray *contentArray = model.DescriptionList.mutableCopy;
    
    CGFloat secondW = 75 * kScale;
    CGFloat firstW = SCREEN_WIDTH - 145 * kScale - 20 - secondW - 12;
    CGFloat H = 22 * kScale;
    for (int i = 0; i < contentArray.count; i++) {
        NSString *str = contentArray[i];
        UILabel *label = [UILabel labelWithTitle:str color:UIColorHex(666666) fontSize:13];
        [self.contentView addSubview:label];
        
        CGFloat top = 10 * kScale + H * (i / 2);
        CGFloat left = 145 * kScale + 10 + firstW * (i % 2);
        CGFloat W = i % 2 == 0 ? firstW : secondW;
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(top);
            make.left.equalTo(self.leftImageView).mas_offset(left);
            make.height.mas_equalTo(H);
            make.width.mas_equalTo(W);
        }];
        [self.labelsArray addObject:label];
    }
    
    CGFloat contentTotalH = 10 * kScale + ceil(contentArray.count / 2.0) * H;
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(contentTotalH + 15 * kScale);
    }];
    
    [self.bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.contentView).mas_offset(-10);
    }];
    
    [self.cancelCollectionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.equalTo(self.bottomLabel);
        make.height.mas_equalTo(22);
    }];
    
    // 加条线
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel);
        make.bottom.equalTo(self.contentView);
        make.right.equalTo(self.contentView).mas_offset(-10);
        make.height.mas_equalTo(1);
    }];
}


- (UIImageView *)leftImageView
{
    if (!_leftImageView) {
        _leftImageView = [[UIImageView alloc] init];
        _leftImageView.contentMode = UIViewContentModeScaleAspectFill;
        _leftImageView.layer.cornerRadius = 10;
        _leftImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_leftImageView];
    }
    return _leftImageView;
}

- (UIImageView *)selloutImageView
{
    if (!_selloutImageView) {
        _selloutImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"homelistsellout"]];
        _selloutImageView.contentMode = UIViewContentModeCenter;
        [self.contentView addSubview:_selloutImageView];
    }
    return _selloutImageView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:[UIColor blackColor] fontSize:15];
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel labelWithTitle:@"￥0.00" color:UIColorHex(FF0000) fontSize:18];
        [self.contentView addSubview:_priceLabel];
    }
    return _priceLabel;
}

- (UIButton *)cancelCollectionBtn
{
    if (!_cancelCollectionBtn) {
        _cancelCollectionBtn = [UIButton createButtonWithTitle:@"取消收藏" titleColor:UIColorHex(999999) titleFontSize:11 borderColor:UIColorHex(999999) cornerRadius:11 contentEdgeInsets:UIEdgeInsetsMake(6, 9, 6, 9) target:self action:@selector(cancelCollectionBtnClick)];
        _cancelCollectionBtn.hidden = YES;
        [self.contentView addSubview:_cancelCollectionBtn];
    }
    return _cancelCollectionBtn;
}

- (UILabel *)bottomLabel
{
    if (!_bottomLabel) {
        _bottomLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:11];
        [self.contentView addSubview:_bottomLabel];
    }
    return _bottomLabel;
}

- (UIView *)lineView
{
    if (!_lineView) {
        _lineView = [[UIView alloc] init];
        _lineView.backgroundColor = kSeparatorColor;
        [self.contentView addSubview:_lineView];
    }
    return _lineView;
}


//- (void)awakeFromNib {
//    [super awakeFromNib];
//
//    self.selectionStyle = UITableViewCellSelectionStyleNone;
//
//    self.rightImageView.layer.cornerRadius = 10;
//
//    self.lookLabel.layer.borderColor = UIColorHex(FFB6AD).CGColor;
//    self.lookLabel.layer.borderWidth = 1;
//    self.lookLabel.layer.cornerRadius = 3;
//
//    self.oftenLabel.layer.borderColor = UIColorHex(CAACFF).CGColor;
//    self.oftenLabel.layer.borderWidth = 1;
//    self.oftenLabel.layer.cornerRadius = 3;
//
//    self.lookLabel.hidden = YES;
//    self.oftenLabel.hidden = YES;
//
//
//
//
//}




@end
